home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C24 / Rtshapes.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.9 KB  |  116 lines

  1. //: C24:Rtshapes.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Counting shapes
  7. #include "../purge.h"
  8. #include <iostream>
  9. #include <ctime>
  10. #include <typeinfo>
  11. #include <vector>
  12. using namespace std;
  13.  
  14. class Shape {
  15. protected:
  16.   static int count;
  17. public:
  18.   Shape() { count++; }
  19.   virtual ~Shape() { count--; }
  20.   virtual void draw() const = 0;
  21.   static int quantity() { return count; }
  22. };
  23.  
  24. int Shape::count = 0;
  25.  
  26. class SRectangle : public Shape {
  27.   void operator=(SRectangle&); // Disallow
  28. protected:
  29.   static int count;
  30. public:
  31.   SRectangle() { count++; }
  32.   SRectangle(const SRectangle&) { count++;}
  33.   ~SRectangle() { count--; }
  34.   void draw() const {
  35.     cout << "SRectangle::draw()" << endl;
  36.   }
  37.   static int quantity() { return count; }
  38. };
  39.  
  40. int SRectangle::count = 0;
  41.  
  42. class SEllipse : public Shape {
  43.   void operator=(SEllipse&); // Disallow
  44. protected:
  45.   static int count;
  46. public:
  47.   SEllipse() { count++; }
  48.   SEllipse(const SEllipse&) { count++; }
  49.   ~SEllipse() { count--; }
  50.   void draw() const {
  51.     cout << "SEllipse::draw()" << endl;
  52.   }
  53.   static int quantity() { return count; }
  54. };
  55.  
  56. int SEllipse::count = 0;
  57.  
  58. class SCircle : public SEllipse {
  59.   void operator=(SCircle&); // Disallow
  60. protected:
  61.   static int count;
  62. public:
  63.   SCircle() { count++; }
  64.   SCircle(const SCircle&) { count++; }
  65.   ~SCircle() { count--; }
  66.   void draw() const {
  67.     cout << "SCircle::draw()" << endl;
  68.   }
  69.   static int quantity() { return count; }
  70. };
  71.  
  72. int SCircle::count = 0;
  73.  
  74. int main() {
  75.   vector<Shape*> shapes;
  76.   srand(time(0)); // Seed random number generator
  77.   const int mod = 12;
  78.   // Create a random quantity of each type:
  79.   for(int i = 0; i < rand() % mod; i++)
  80.     shapes.push_back(new SRectangle);
  81.   for(int j = 0; j < rand() % mod; j++)
  82.     shapes.push_back(new SEllipse);
  83.   for(int k = 0; k < rand() % mod; k++)
  84.     shapes.push_back(new SCircle);
  85.   int nCircles = 0;
  86.   int nEllipses = 0;
  87.   int nRects = 0;
  88.   int nShapes = 0;
  89.   for(int u = 0; u < shapes.size(); u++) {
  90.     shapes[u]->draw();
  91.     if(dynamic_cast<SCircle*>(shapes[u]))
  92.       nCircles++;
  93.     if(dynamic_cast<SEllipse*>(shapes[u]))
  94.       nEllipses++;
  95.     if(dynamic_cast<SRectangle*>(shapes[u]))
  96.       nRects++;
  97.     if(dynamic_cast<Shape*>(shapes[u]))
  98.       nShapes++;
  99.   }
  100.   cout << endl << endl
  101.     << "Circles = " << nCircles << endl
  102.     << "Ellipses = " << nEllipses << endl
  103.     << "Rectangles = " << nRects << endl
  104.     << "Shapes = " << nShapes << endl
  105.     << endl
  106.     << "SCircle::quantity() = "
  107.     << SCircle::quantity() << endl
  108.     << "SEllipse::quantity() = "
  109.     << SEllipse::quantity() << endl
  110.     << "SRectangle::quantity() = "
  111.     << SRectangle::quantity() << endl
  112.     << "Shape::quantity() = "
  113.     << Shape::quantity() << endl;
  114.   purge(shapes);
  115. } ///:~
  116.